package com.wang.jmonkey.ca.filter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.wang.jmonkey.ca.property.LoginFilterProperty;
import com.wang.jmonkey.ca.property.SecurityProperty;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 基础登陆拦截器
 * @Author HeJiawang
 * @Date 2021/5/8 16:37
 */
public abstract class AbstractAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    protected AbstractAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher , AuthenticationManager authenticationManager) {
        super(requiresAuthenticationRequestMatcher);
        super.setAuthenticationManager(authenticationManager);
    }

    protected static String getLoginUrl(SecurityProperty securityProperty , Class<?> clazz) {
        List<LoginFilterProperty> list = securityProperty.getLoginFilters();
        if (CollUtil.isEmpty(list)) return StrUtil.EMPTY;

        for (LoginFilterProperty filter : list) {
            if (ObjectUtils.isEmpty(filter)) continue;

            try {
                Class<?> filterClass = ClassUtils.forName(filter.getFilter(), ClassUtils.getDefaultClassLoader());
                if (clazz.isAssignableFrom(filterClass)) {
                    return filter.getLoginUrl();
                }
            } catch (ClassNotFoundException | LinkageError e) {
                e.printStackTrace();
            }
        }

        return StrUtil.EMPTY;
    }

    protected static String getMethod(SecurityProperty securityProperty , Class<?> clazz) {
        List<LoginFilterProperty> list = securityProperty.getLoginFilters();
        if (CollUtil.isEmpty(list)) return StrUtil.EMPTY;

        for (LoginFilterProperty filter : list) {
            if (ObjectUtils.isEmpty(filter)) continue;

            try {
                Class<?> filterClass = ClassUtils.forName(filter.getFilter(), ClassUtils.getDefaultClassLoader());
                if (clazz.isAssignableFrom(filterClass)) {
                    return filter.getMethod().toUpperCase();
                }
            } catch (ClassNotFoundException | LinkageError e) {
                e.printStackTrace();
            }
        }

        return StrUtil.EMPTY;
    }

    protected boolean checkValidateCode(String code) {
        return StrUtil.isEmpty(code);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException, IOException, ServletException {
        try {

            cacheAuthenticationFilter(this);
            return authentication(request, response);
        } catch (AuthenticationException e) {
            this.getFailureHandler().onAuthenticationFailure(request, response, e);
        }

        return null;
    }

    private void cacheAuthenticationFilter(Object object) {
        if (!ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes())) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            if (!ObjectUtils.isEmpty(request)) {
                request.setAttribute(object.getClass().getSimpleName() , object);
            }
        }
    }

    protected abstract Authentication authentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException;
}
